package com.sunrin.applepie.gm_player.activity;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.ToggleButton;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

public class MusicPlayActivity extends Activity implements Runnable,
		OnClickListener {

	boolean repeat = false; // repeated regeneration
	private MediaPlayer mp;
	private Button mBack, mFront, mPlay, mRandom;
	private ToggleButton mRepeat;
	private SeekBar mSeekBar;
	public boolean stream=false;
	private Thread th = new Thread(MusicPlayActivity.this);
	String path = null;// mp3file
	String ID = null;
	String Title = null;
	// directory

	String mp3file_name = null; // mp3file name

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.musicplayactivity);

		mp = new MediaPlayer();
		mFront = (Button) findViewById(R.id.front);
		mBack = (Button) findViewById(R.id.back);
		mPlay = (Button) findViewById(R.id.Play);
		mRandom = (Button) findViewById(R.id.random);
		mRepeat = (ToggleButton) findViewById(R.id.repeat);
		mSeekBar = (SeekBar) findViewById(R.id.seekBar);

		mFront.setOnClickListener(this);
		mBack.setOnClickListener(this);
		mPlay.setOnClickListener(this);
		mRandom.setOnClickListener(this);
		mRepeat.setOnClickListener(this);
		mSeekBar.setOnClickListener(this);

		Intent i = getIntent();
		path = i.getStringExtra("path");
		ID = i.getStringExtra("songs_id");
		Title = i.getStringExtra("songs_name");
		
		Log.d("fuck", path + "," + mp3file_name);

		try {
			mp.setDataSource(path);

			// play music
			mp.prepare();
			th.start();
			mSeekBar.setVisibility(ProgressBar.VISIBLE);
			mSeekBar.setProgress(0);
			mSeekBar.setMax(mp.getDuration());// as maximum value as music's
			// // length
			int kk = mSeekBar.getMax();
			int m = (kk/60000);
			int s = (kk%60000)/1000;
			mSeekBar.setOnSeekBarChangeListener(onseek);

		} catch (Exception e) {
			// TODO : handle exception
		}
		mp.setOnCompletionListener(new OnCompletionListener() {
			
			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub
				mSeekBar.setProgress(0);
				
			}
		});
	}
	public void run() {
		// TODO Auto-generated method stub
		int current = 0;
		while(mp!=null){
			try{
				Thread.sleep(100000);
				current=mp.getCurrentPosition();
				if(mp.isPlaying()){
					mSeekBar.setProgress(current);
				}
			} catch(Exception e){
			e.printStackTrace();	
			}
		}
	}
	OnSeekBarChangeListener onseek = new OnSeekBarChangeListener() {
		
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}
		
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}
		
		public void onProgressChanged(SeekBar mSeekBar, int progress,
				boolean fromUser) {
			mp.seekTo(progress);
			mSeekBar.setProgress(progress);
			int m = progress/60000;
			int s =(progress%60000)/1000;
			// TODO Auto-generated method stub
		}
	};

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.Play:
			if (mp.isPlaying()) {
				mp.pause();
				stream=true;
			} else {
				mp.start();
				stream=false;
			}
			break;
		case R.id.front:
			// go to next music
			//
			break;
		case R.id.back:
			// go to back music
			//
			break;
		case R.id.random:

			break;
		case R.id.repeat:
			// repeated regeneration
			if (mp.isLooping()) {
				mp.setLooping(false);
			} else {
				mp.setLooping(true);
			}
			break;
		}
	}



}